var ACTION = require("../../base/action");
var utils = require("../../utils/utils");
var time = require("../../utils/time");
var ocrGml = require("../../utils/ocrGml");

function getActionSleepGet(config, ad, contentType) {
  var actionSleepGet = new ACTION(contentType, config);
  actionSleepGet.goStart = function () {
    var ocrItem = ocrGml.captureExistOne("睡觉赚钱", true);
    if (ocrItem) {
      this.execFCount++;
      this.updateStorage();
      utils.clickOcrItem(ocrItem);
      sleep(utils.getRandom(2.5, 3, 1000));
    }
    return ocrItem;
  };
  actionSleepGet.beforeExec = function () {
    var content = "我睡醒了";
    if (utils.clickTextStartsWith(content)) {
      this.execSCount++;
      this.updateStorage();
      sleep(utils.getRandom(2, 3, 1000));
      return true;
    }
    var content = "后开启";
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      this.execSCount++;
      this.updateStorage();
      this.afterExec();
    }
    var content = "后可开启";
    var UIText = utils.existsTextEndsWith(content);
    if (UIText) {
      this.execSCount++;
      this.updateStorage();
      this.afterExec();
    }
    return false;
  };
  actionSleepGet.exec = function () {
    var contents = [
      //prettier-ignore
      "领取",
    ];
    var UIText = utils.existsOneTextStartsWith(contents);
    if (UIText) {
      utils.clickUIText(UIText);
      sleep(utils.getRandom(2, 3, 1000));
      var contents = ["开心收下", "立即收下"];
      var ocrItem = ocrGml.captureExistOne(contents, true);
      if (ocrItem) {
        utils.clickOcrItem(ocrItem);
        sleep(utils.getRandom(2, 3, 1000));
        return;
      }
      var contents = ["看视频领", "看广告领", "看视频再领"];
      var ocrItem = ocrGml.captureExistOne(contents, true);
      if (ocrItem) {
        utils.clickOcrItem(ocrItem);
        sleep(utils.getRandom(2, 3, 1000));
        ad.init(1, function () {
          var ocrItem = ocrGml.captureExistOne(["收益记录", "活动规则"]);
          return ocrItem;
        });
      }
    }
  };
  actionSleepGet.afterExec = function () {
    console.log(this.name, time.str(), "afterExec");
    back();
    sleep(utils.getRandom(2, 3, 1000));
  };
  return actionSleepGet;
}

module.exports = getActionSleepGet;
